package com.clkj.sunhelp.mvp.releaseyear;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.adapter.list.ListAdapterForReleaseYearChoose;
import com.clkj.sunhelp.bean.YearItem;
import com.clkj.sunhelp.common.Constant;
import com.clkj.sunhelp.mvp.base.BaseDialogFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/3/29.
 */

public class DialogFragmentToChooseReleaseYear extends BaseDialogFragment {


    public static final String ARGUMENT = "argument";

    private String mArgument;


    public static DialogFragmentToChooseReleaseYear getInstance(String argument) {
        DialogFragmentToChooseReleaseYear dialogFragmentToChooseReleaseYear = new DialogFragmentToChooseReleaseYear();
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT, argument);

        dialogFragmentToChooseReleaseYear.setArguments(bundle);
        return dialogFragmentToChooseReleaseYear;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            mArgument = bundle.getString(ARGUMENT);

        }
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    private TextView tvTip;
    private ListView lv;

    List<YearItem> mYearList = new ArrayList<>();
    ListAdapterForReleaseYearChoose mAdapter;


    ReleaseYearChooseListener mReleaseYearChooseListener;

    public ReleaseYearChooseListener getReleaseYearChooseListener() {
        return mReleaseYearChooseListener;
    }

    public void setReleaseYearChooseListener(ReleaseYearChooseListener releaseYearChooseListener) {
        mReleaseYearChooseListener = releaseYearChooseListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.dialog_frag_release_year_choose, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        assignView(view);
        initView();
    }


    @Override
    public void initData() {
        Collections.addAll(mYearList, Constant.RELEASE_YEAR_ARRAY);

    }

    @Override
    public void assignView(View view) {
        tvTip = (TextView) view.findViewById(R.id.tv_tip);
        lv = (ListView) view.findViewById(R.id.lv);
    }

    @Override
    public void initView() {
        tvTip.setText("发放年度选择：");

        mAdapter = new ListAdapterForReleaseYearChoose(mYearList, getActivity());
        lv.setAdapter(mAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != mReleaseYearChooseListener) {
                    mReleaseYearChooseListener.onReleaseYearChoose(mYearList.get(position));
                }

                dismiss();
            }
        });
    }


    public interface ReleaseYearChooseListener {
        public void onReleaseYearChoose(YearItem chooseYearItem);
    }
}
